/*****************************************************************
*
*	Netcare Agent Project
*		Copyright(C) 2010, Wicresoft EKA. All Rights Reserved.
*
*	Authors:
*		Miles Chen (milesch@wicresoft.com)
*		TingRong Shi (trshi@wicresoft.com)
*
*	Part of the product code depends on gSOAP under GPL license
*		Copyright(C) 2000-2010, Robert van Engelen, Genivia Inc. All Rights Reserved.
*
*****************************************************************/
#include <cstdio>
#include <cstdarg>

#include "Log.h"
#include "ConsoleLogListener.h"
#include "TextWriterLogListener.h"
#include "misc.h"

ALCATEL_SMS;

USING_STD;

#ifndef MAX_CHAR
#define MAX_CHAR 512
#endif


CLog CLog::_instance;
CLog::CLog(void)
{
	InitializeApp();

	_listeners.push_back(new CConsoleLogListener());

#ifdef WIN32
	wchar_t* logFile = GetLoggerPath();
#else
	char* logFile = GetLoggerPath();
#endif
	_listeners.push_back(new CTextWriterLogListener(logFile));
	delete[] logFile;

	_bufferW = new wchar_t[MAX_CHAR];
	_buffer = new char[MAX_CHAR];
}

CLog::~CLog(void)
{
	for(int i=0; i<(int)_listeners.size(); i++)
	{
		delete _listeners[i];
	}
	_listeners.clear();
	delete[] _bufferW;
	delete[] _buffer;

	UninitializeApp();
}

const CLog* CLog::Current()
{
	return &_instance;
}

void CLog::WriteInfo() const
{
	WriteInfo("");
}
void CLog::WriteInfo(const wchar_t* format, ...) const
{
	va_list ap;
	va_start(ap, format);
	vswprintf(_bufferW, MAX_CHAR, format, ap);
	va_end(ap);

	for(int i=0; i<(int)_listeners.size(); i++)
	{
		_listeners[i]->WriteLine("Info", _bufferW);
	}
}
void CLog::WriteInfo(const char* format, ...) const
{
	va_list ap;
	va_start(ap, format);
	vsprintf(_buffer, format, ap);
	va_end(ap);

	for(int i=0; i<(int)_listeners.size(); i++)
	{
		_listeners[i]->WriteLine("Info", _buffer);
	}
}

#ifdef _DEBUG
void CLog::WriteDebug() const
{
	WriteDebug("");
}
void CLog::WriteDebug(const wchar_t* format, ...) const
{
	va_list ap;
	va_start(ap, format);
	vswprintf(_bufferW, MAX_CHAR, format, ap);
	va_end(ap);

	for(int i=0; i<(int)_listeners.size(); i++)
	{
		_listeners[i]->WriteLine("Debug", _bufferW);
	}
}
void CLog::WriteDebug(const char* format, ...) const
{
	va_list ap;
	va_start(ap, format);
	vsprintf(_buffer, format, ap);
	va_end(ap);

	for(int i=0; i<(int)_listeners.size(); i++)
	{
		_listeners[i]->WriteLine("Debug", _buffer);
	}
}
#else
void CLog::WriteDebug() const
{
}
void CLog::WriteDebug(const wchar_t* format, ...) const
{
}
void CLog::WriteDebug(const char* format, ...) const
{
}
#endif
void CLog::WriteError() const
{
	WriteError("");
}
void CLog::WriteError(const wchar_t* format, ...) const
{
	va_list ap;
	va_start(ap, format);
	vswprintf(_bufferW, MAX_CHAR, format, ap);
	va_end(ap);

	for(int i=0; i<(int)_listeners.size(); i++)
	{
		_listeners[i]->WriteLine("Error", _bufferW);
	}
}
void CLog::WriteError(const char* format, ...) const
{
	va_list ap;
	va_start(ap, format);
	vsprintf(_buffer, format, ap);
	va_end(ap);

	for(int i=0; i<(int)_listeners.size(); i++)
	{
		_listeners[i]->WriteLine("Error", _buffer);
	}
}

ENDNS ENDNS